package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.FinanceAccountRecordEntity;
import com.chushouya.manager.dao.mapper.FinanceAccountRecordEntityMapper;
import com.chushouya.manager.dto.admin.finance.FinanceAccountRecordQuery;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class FinanceAccountRecordRepository extends BaseRepository<FinanceAccountRecordEntityMapper, FinanceAccountRecordEntity, Long> {

    public FinanceAccountRecordEntity selectOne(FinanceAccountRecordQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public PageResult<FinanceAccountRecordEntity> selectPageResult(FinanceAccountRecordQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }

    private Example buildExample(FinanceAccountRecordQuery query) {
        final Example example = this.buildExample();
        Example.Criteria c = example.and();

        if (Objects.nonNull(query.getAccountUserType())) {
            c.andEqualTo("accountUserType", query.getAccountUserType());
        }
        if (Longs.notNullAndZero(query.getAccountUserId())) {
            c.andEqualTo("accountUserId", query.getAccountUserId());
        }
        if (Longs.notNullAndZero(query.getAccountId())) {
            c.andEqualTo("accountId", query.getAccountId());
        }
        if (Objects.nonNull(query.getTradeStatus())) {
            c.andEqualTo("tradeStatus", query.getTradeStatus());
        }
        if (Strings.isNotEmpty(query.getTradeNo())) {
            c.andEqualTo("tradeNo", query.getTradeNo());
        }
        if (Strings.isNotEmpty(query.getOutTradeNo())) {
            c.andEqualTo("outTradeNo", query.getOutTradeNo());
        }
        if (Strings.isNotEmpty(query.getBizNo())) {
            c.andEqualTo("bizNo", query.getBizNo());
        }
        if(Objects.nonNull(query.getBizType())){
            c.andEqualTo("bizType", query.getBizType());
        }
        if (Lists.isNotEmpty(query.getBizTypes())) {
            c.andIn("bizType", query.getBizTypes());
        }
        if (Objects.nonNull(query.getAccountType())) {
            c.andEqualTo("accountType", query.getAccountType());
        }
        if (Objects.nonNull(query.getTradeTimeStart()) && Objects.nonNull(query.getTradeTimeEnd())) {
            c.andBetween("tradeTime", query.getTradeTimeStart(), Dates.getLastTime(query.getTradeTimeEnd()));
        }
        if (Strings.isNotEmpty(query.getKeyword())) {
            c.andLike("bizNo", Strings.format("%{}%", query.getKeyword()));
            c.andLike("bizRemark", Strings.format("%{}%", query.getKeyword()));
        }
        example.orderBy("recordId").desc();

        return example;
    }
}
